package ver0.blur;

import java.awt.image.BufferedImage;

import ver0.blur.handlers.BlurHandler;
import ver0.util.MDGauss;

public class Blur {
	
	private BlurHandler handler;
	
	public Blur(BlurHandler handler){
		this.handler=handler;
	}

	public BufferedImage blur2D(BufferedImage image,int sigma,int radius){
		int width=image.getWidth();
		int height=image.getHeight();
		int[][] data=new int[width][height];
		double[][] matrix=MDGauss.get2DMatrix(sigma, radius);
		matrix=MDGauss.normalize2D(matrix);
		for(int x=0;x<width;x++){
			for(int y=0;y<height;y++){
				data[x][y]=handler.blurAPixel(image, x, y, radius,matrix);
			}
		}
		for(int x=0;x<width;x++){
			for(int y=0;y<height;y++){
				image.setRGB(x, y, data[x][y]);
			}
		}
		return image;
	}
	
	public BufferedImage blur1D(BufferedImage image,int sigma,int radius){
		int width=image.getWidth();
		int height=image.getHeight();
		int[][] data=new int[width][height];
		double[] array=MDGauss.get1DArray(sigma, radius);
		MDGauss.normalize1D(array);
		for(int x=0;x<width;x++){
			for(int y=0;y<height;y++){
				data[x][y]=handler.blurAPixelInX(image, x, y, radius, array);
			}
		}
		for(int x=0;x<width;x++){
			for(int y=0;y<height;y++){
				image.setRGB(x, y, data[x][y]);
			}
		}
		for(int x=0;x<width;x++){
			for(int y=0;y<height;y++){
				data[x][y]=handler.blurAPixelInY(image, x, y, radius, array);
			}
		}
		for(int x=0;x<width;x++){
			for(int y=0;y<height;y++){
				image.setRGB(x, y, data[x][y]);
			}
		}
		return image;
	}

}
